package com.bright.ghj.overt.service.impl;

import com.bright.ghj.common.pojo.dto.PageDTO;
import com.bright.ghj.common.pojo.vo.PageVO;
import com.bright.ghj.common.util.PageUtil;
import com.bright.ghj.common.util.StringUtil;
import com.bright.ghj.overt.constant.SystemConstant;
import com.bright.ghj.overt.converter.DistConverter;
import com.bright.ghj.overt.manager.DistZtManager;
import com.bright.ghj.overt.pojo.dto.DistDTO;
import com.bright.ghj.overt.pojo.dto.ZtAdaptDTO;
import com.bright.ghj.overt.pojo.dto.ZtDTO;
import com.bright.ghj.overt.pojo.query.DistQuery;
import com.bright.ghj.overt.pojo.vo.DistVO;
import com.bright.ghj.overt.pojo.vo.ZtAdaptVO;
import com.bright.ghj.overt.service.DistService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author hxj
 * @Date 2024/1/29 17:02
 * @Description
 */
@Component
@RequiredArgsConstructor
public class DistServiceImpl implements DistService {

    private final DistZtManager distZtManager;

    private final DistConverter distConverter;

    @Override
    public List<DistVO> listDistTrees(DistQuery distQuery) {
        int nextLength;
        // v1.0.2 前端在遇到多地区时传值出错 暂时由后端处理 通过传进来的distNo是否有逗号判断是否为多地区
        boolean isMultiDists = StringUtil.contains(distQuery.getDistNo(), ",");
        String distNo = distQuery.getDistNo();
        // 地区树类型
        int type = distQuery.getType();

        List<String> userDistNo = distQuery.getUserDistNo();
        // 要查找的地区编号 兼容多地区情形
        List<String> queryDistNos;
        if (StringUtil.isEmpty(distNo) || isMultiDists) {
            // 初次加载未选地区的情形 使用用户最顶级地区返回
            queryDistNos = userDistNo;
            nextLength = userDistNo.size() == 0 ? 4 : distZtManager.getNextLength(userDistNo.get(0));
        } else {
            queryDistNos = new ArrayList<>();
            queryDistNos.add(distNo);
            nextLength = distZtManager.getNextLength(distNo);
        }

        List<DistVO> distVOS = new ArrayList<>();
        for (String queryDistNo : queryDistNos) {
            if (StringUtil.isNotEmpty(queryDistNo)) {
                // 把当前查询的地区号的地区放在第一位
                DistDTO parentDistDTO = distZtManager.getDistByDistNoByCache(queryDistNo);
                if (parentDistDTO != null) {
                    distVOS.add(distConverter.dtoConvertVO(parentDistDTO));
                }
            }

            // 获取下一级地区/账套
            // v1.0.2 如果是多地区的情形 地区树会异常 改成只返回一级地区 不返回下级
            if (!isMultiDists) {
                if (nextLength == 12) {
                    // 获取账套
                    DistQuery ztDistQuery = new DistQuery();
                    ztDistQuery.setDistNo(queryDistNo);
                    ztDistQuery.setUserDistNo(distQuery.getUserDistNo());
                    ztDistQuery.setUserZtId(distQuery.getUserZtId());
                    List<ZtDTO> zts = distZtManager.listZtByDistNo(ztDistQuery);

                    for (ZtDTO ztDTO : zts) {
                        DistVO distVO = new DistVO();
                        distVO.setDistNo(ztDTO.getZtId());
                        distVO.setDistName(ztDTO.getZtJc());
                        distVO.setParentDistNo(ztDTO.getDistNo());
                        distVO.setHasNext(false);
                        distVOS.add(distVO);
                    }

                } else {
                    // 获取地区
                    List<DistDTO> dists = distZtManager.listDistByDistNoAndLength(queryDistNo, nextLength, userDistNo);
                    List<DistVO> subDistVOS = distConverter.dtoConvertVO(dists);

                    // 如果地区树不展示账套 则村级地区hasNext为false
                    if (nextLength == 9 && type == SystemConstant.DIST_TREE_NOT_SHOW_ZT) {
                        for (DistVO distVO : subDistVOS) {
                            distVO.setHasNext(false);
                        }
                    }
                    distVOS.addAll(subDistVOS);
                }
            }
        }

        return distVOS;
    }

    @Override
    public PageVO<ZtAdaptVO> listZtAdapt(DistQuery distQuery) {
        List<ZtAdaptVO> ztAdaptVOS = new ArrayList<>();

        PageDTO<ZtDTO> ztDTOS = distZtManager.pageZtByDistNo(distQuery);
        for (ZtDTO ztDTO : ztDTOS.getData()) {
            ZtAdaptVO ztAdaptVO = new ZtAdaptVO();
            ztAdaptVO.setId(ztDTO.getId());
            ztAdaptVO.setZtId(ztDTO.getZtId());
            ztAdaptVO.setZtName(ztDTO.getZtName());
            ztAdaptVO.setDistNo(ztDTO.getDistNo());
            ztAdaptVO.setZthImport(ztDTO.getZthImport());
            ztAdaptVO.setZtNameImport(ztDTO.getZtNameImport());
            ztAdaptVOS.add(ztAdaptVO);
        }

        PageVO<ZtAdaptVO> pageVO = new PageVO<>();
        pageVO.setData(ztAdaptVOS);
        pageVO.setTotal(ztDTOS.getTotal());
        pageVO.setPageCount(ztDTOS.getPageCount());

        return pageVO;
    }

    @Override
    public PageVO<ZtDTO> pageZt(DistQuery distQuery) {
        PageDTO<ZtDTO> ztDTOPageDTO = distZtManager.pageZtByDistNo(distQuery);
        PageVO<ZtDTO> pageVO = new PageVO<>();
        pageVO.setData(ztDTOPageDTO.getData());
        pageVO.setTotal(ztDTOPageDTO.getTotal());
        pageVO.setPageCount(ztDTOPageDTO.getPageCount());

        return pageVO;
    }

    @Override
    public List<ZtDTO> listZt(DistQuery distQuery) {
        return distZtManager.listZtByDistNo(distQuery);
    }


    @Override
    public void saveZtAdapt(ZtAdaptDTO ztAdaptDTO) {
//        ZtDTO ztDTO = distZtManager.getZtByZtIdByCache(ztAdaptDTO.getZtId());
//        ztDTO.setZthImport(ztAdaptDTO.getZthImport());
//        ztDTO.setZtNameImport(ztAdaptDTO.getZtNameImport());
        distZtManager.saveZtAdapt(ztAdaptDTO);
    }
}
